package com.ingenic.glass.camera.util;

import com.ingenic.glass.camera.CameraAppImpl;
import com.ingenic.glass.camera.Storage;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class StorageSpaceUtil {

	private static final String TAG = "StorageSpaceUtil";

	public static final long UNAVAILABLE = -1L;
    public static final long PREPARING = -2L;
    public static final long UNKNOWN_SIZE = -3L;
    // low storage threshold
    private static final long COLDWAVE_LOW_STORAGE_THRESHOLD = 1 << 28; // 256 MB
    private static final long CRUISE_LOW_STORAGE_THRESHOLD = 100000000;
    public static final long PICTURE_SIZE = 1500000;

    public static long getAvailableSpace() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_CHECKING.equals(state)) {
            return PREPARING;
        }
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            return UNAVAILABLE;
        }

        try {
            StatFs stat = new StatFs(Storage.BASE_DIR);
	    long low_storage_threshold = 0;
	    if (CameraAppImpl.IS_CRUISE_BOARD)
		    low_storage_threshold = CRUISE_LOW_STORAGE_THRESHOLD;
	    else
		    low_storage_threshold = COLDWAVE_LOW_STORAGE_THRESHOLD;
            return (stat.getAvailableBlocks() * (long) stat.getBlockSize()) - low_storage_threshold;
        } catch (Exception e) {
            Log.e(TAG, "Fail to access external storage", e);
        }
        return UNKNOWN_SIZE;
    }

}
